<?php
session_start();
/*
* Issue summones makes an emergency, and sets the db up for use.  
*/

/************************
* User variables to set.
* $link - the link displayed in the summons email
* $from - the address that will appear in the 'from' field of the summons email
* $subect - The subject line of the summons email
************************/

$link = 'http://flightdeck.ie.orst.edu/ECD/index.php?page=team_status.php';
$from = 'From: Admin@ECFweb.org';
$subject = 'Emergency Cesarean Delivery Required - Response Needed';

/*
* Begin code in proper
*/
$_SESSION['dbhost'] = 'engr-db.engr.oregonstate.edu:3307';
$_SESSION['dbname'] = 'funkecd';
$_SESSION['dbuser'] = 'funkecd';
$_SESSION['dbpass'] = 'ktrDtj1b';

$mysql_handle = mysql_connect($_SESSION['dbhost'], $_SESSION['dbuser'], $_SESSION['dbpass'])
    or die("Error connecting to database server");

mysql_select_db($_SESSION['dbname'], $mysql_handle)
    or die("Error selecting database: $dbname");

//internal variables being set.
$email = array();
$roles = array();
$i = 1;
$delay = 60;

//getting team member information
$email[$i++] = $_POST['Obstetrician'];
$email[$i++] = $_POST['Surgical_Assistant'];
$email[$i++] = $_POST['Anesthetist'];
$email[$i++] = $_POST['OB_Nurse'];
$email[$i++] = $_POST['Scrub_Nurse'];
$email[$i++] = $_POST['Circulating_Nurse'];
$email[$i++] = $_POST['Physician'];
$email[$i++] = $_POST['Therapist'];
$regEmail = '^[a-zA-Z0-9._%-]{1,50}@[a-zA-Z0-9.-]{1,50}\.[a-zA-Z]{2,4}$';
$query = "SELECT `role` FROM `Team_Roles`";
$result = mysql_query($query);
$i = 1;
while($row = mysql_fetch_array($result))
{
	$roles[$i] = $row['role'];
	$role = $roles[$i];
	$_SESSION["$role"] = 1;
	$i++;
}
$_SESSION['roles'] = $roles;

//time for the different 'calls' being set
$time1 = time();
$time2 = ($time1 + $delay);
$time3 = ($time2 + $delay);

//getting emergency information
$pid = $_POST['pid'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$status = $_POST['status'];
$reason_for_cs= $_POST['reason_for_cs'];
$lid = $_POST['lid'];
$query = "INSERT INTO `Emergency` (`emid`, `etid`, `emergency_status`) VALUES (NULL, '1', '$status')";
$result = mysql_query($query);
$equery = "SELECT * FROM `Emergency` ORDER BY `emid` DESC;";
$result = mysql_query($equery);
$row = mysql_fetch_array($result);
$emid = $row['emid'];
$estat = $row['emergency_status'];
$_SESSION['emid'] = $emid;
$lquery = "INSERT INTO `Emergency_Locations` (`lid` ,`emid`) 
VALUES ('$lid', '$emid');";
$result = mysql_query($lquery);

//logic to add team members to emergency list in order
for($i = 1; $i < 9; $i++)
{
	$e = $email[$i];
	$query = "SELECT `tmid` FROM  `Team_Member_List` where `member_email` = '$e'";
	$result = mysql_query($query);
	$team = mysql_fetch_array($result);
	$tmid1 = $team['tmid'];
	
	$query = "Select * FROM `Call_List` where `tmid` = '$tmid1'";
	$result = mysql_query($query);
	$firstCall = mysql_fetch_array($result);
		
	if($firstCall['call_number'] == 1)
	{
		$query = "Select `tmid` FROM `Call_List` where `trid` = '$i' and `call_number` = 2";
		$result = mysql_query($query);
		$team = mysql_fetch_array($result);
		$tmid2 = $team['tmid'];
		
		$query = "Select `tmid` FROM `Call_List` where `trid` = '$i' and `call_number` = 3";
		$result = mysql_query($query);
		$team = mysql_fetch_array($result);
		$tmid3 = $team['tmid'];
	} 
	else if ($firstCall['call_number'] == 2)
	{
		$query = "Select `tmid` FROM `Call_List` where `trid` = '$i' and `call_number` = 3";
		$result = mysql_query($query);
		$team = mysql_fetch_array($result);
		$tmid2 = $team['tmid'];
		
		$query = "Select `tmid` FROM `Call_List` where `trid` = '$i' and `call_number` = 1";
		$result = mysql_query($query);
		$team = mysql_fetch_array($result);
		$tmid3 = $team['tmid'];
	} 
	else if ($firstCall['call_number'] == 3)
	{
		$query = "Select `tmid` FROM `Call_List` where `trid` = '$i' and `call_number` = 1";
		$result = mysql_query($query);
		$team = mysql_fetch_array($result);
		$tmid2 = $team['tmid'];
		
		$query = "Select `tmid` FROM `Call_List` where `trid` = '$i' and `call_number` = 2";
		$result = mysql_query($query);
		$team = mysql_fetch_array($result);
		$tmid3 = $team['tmid'];
	} 

	//insert team members into the emergency team list in order of call priority
	$query = "INSERT INTO  `Emergency_Team_Assembly_Status` (`emid` ,`trid` ,`tmid` ,`status`, `rpri`, `callTime`)
		VALUES ('$emid',  '$i',  '$tmid1',  '4', '1', '$time1');";		
	$result = mysql_query($query);
	
	$query = "INSERT INTO  `Emergency_Team_Assembly_Status` (`emid` ,`trid` ,`tmid` ,`status`, `rpri`, `callTime`)
		VALUES ('$emid',  '$i',  '$tmid2',  '0', '2', '$time2');";		
	$result = mysql_query($query);
	
	$query = "INSERT INTO `Emergency_Team_Assembly_Status` (`emid` ,`trid` ,`tmid` ,`status`, `rpri`, `callTime`)
		VALUES ('$emid',  '$i',  '$tmid3',  '0', '3', '$time3');";		
	$result = mysql_query($query); 
}


if($pid != null) //try and recover the user from the db
{
	$fpquery = "SELECT `pid` , `patient_first_name` , `patient_last_name`
	FROM `Paient_List`WHERE pid = $pid";
	$result = mysql_query($fpquery);
	$row = mysql_fetch_array($result);
}
else //create a new patient
{
	$piquery = "
	INSERT INTO `Patient_List` (
	`pid` ,	
	`patient_first_name` ,
	`patient_last_name` ,
	`patient_address` ,
	`patient_phone` ,
	`patient_email` ,
	`patient_sex` ,
	`patient_birthdate`
	)
	VALUES (
	NULL , '$fname', '$lname', '', '', '', '', ''
	);";

	$result = mysql_query($piquery);

	$pidquery = "SELECT `pid` FROM `Patient_List` 
	WHERE patient_first_name = '$fname' and patient_last_name = '$lname'
	ORDER BY `pid` DESC";

	$result = mysql_query($pidquery);
	$row = mysql_fetch_array($result);
	$pid = $row['pid'];

}	

//adding the patient to the emergency
$epquery = "INSERT INTO `Emergency_Patient_List` (`pid` ,`emid`)
VALUES ('$pid', '$emid');";
$result = mysql_query($epquery);

//set the OR and patient prep statues to 0
$query = "UPDATE  `Patient_Status_Procedure` SET  `status` =  '0' WHERE `ppid` != 0 ; ";
$result = mysql_query($query);
$query = "UPDATE  `Location_Preparation_Procedure` SET  `status` =  '0' WHERE  `lpid` !=0;";
$result = mysql_query($query);

//getting the 'arraive by' time for the summons email.
$time = time();
$hours = date("G");
$minutes = date("i") + 30;
if($minutes > 59) {
	$minutes = $minutes % 60;
	
	$hours = $hours + 1;

	if($hours > 23)
		$hours = $hours % 24;
	if($minutes < 10)
		$zero = "0";
	else
		$zero = "";
}
$seconds = date("s");
$dt = date("$hours:$zero$minutes:s");

//setting the session variables for the emergency cesarean team page
$_SESSION['pid'] = $pid;

//getting the location name for the email
$query = "SELECT `location_name` FROM `Location_List` WHERE `lid` = $lid";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$location = $row['location_name'];

mysql_close($mysql_handle);

if($fname == "" && $lname == "")
	$fname = "no name given";
if($status == "")
	$status = "no status given";
	
$to = "";
$bodyText = "Emergency, Patient: $fname $lname is in need of a Cesarean Delivery with status: $status. Assemble at $location by $dt";
$message = "$bodyText Use the following link within 3 minutes to report your status: $link";

for($i = 1; $i < 9; $i++)
{
	if(ereg($regEmail, $email[$i]))
	{
		$to = $email[$i];
		$sent = mail($to, $subject, $message, $from);
		$to = 'joefillmore@gmail.com';
	}	
}
header( 'Location: index.php?page=team_status.php' ) ;
?>
